package dockercompose

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestDiscoverManifests(t *testing.T) {

	testdata := []struct {
		name              string
		rootDir           string
		digest            bool
		expectedPipelines []string
	}{
		{
			name:    "Scenario 1 - digest",
			rootDir: "testdata",
			digest:  true,
			expectedPipelines: []string{`name: 'deps(dockercompose): bump "jenkinsci/jenkins" digest'
sources:
  jenkins-lts:
    name: 'get latest image tag for "jenkinsci/jenkins"'
    kind: 'dockerimage'
    spec:
      image: 'jenkinsci/jenkins'
      tagfilter: '^\d*(\.\d*){2}-alpine$'
      versionfilter:
        kind: 'semver'
        pattern: '>=2.150.1-alpine'
  jenkins-lts-digest:
    name: 'get latest image "jenkinsci/jenkins" digest'
    kind: 'dockerdigest'
    spec:
      image: 'jenkinsci/jenkins'
      tag: '{{ source "jenkins-lts" }}'
    dependson:
      - 'jenkins-lts'
targets:
  jenkins-lts:
    name: 'deps: update Docker image "jenkinsci/jenkins" to "{{ source "jenkins-lts" }}"'
    kind: 'yaml'
    spec:
      file: 'docker-compose.yaml'
      key: '$.services.jenkins-lts.image'
    sourceid: 'jenkins-lts-digest'
    transformers:
      - addprefix: 'jenkinsci/jenkins:'
`, `name: 'deps(dockercompose): bump "jenkinsci/jenkins" digest'
sources:
  jenkins-weekly:
    name: 'get latest image tag for "jenkinsci/jenkins"'
    kind: 'dockerimage'
    spec:
      architecture: 'amd64'
      image: 'jenkinsci/jenkins'
      tagfilter: '^\d*(\.\d*){1}-alpine$'
      versionfilter:
        kind: 'semver'
        pattern: '>=2.254-alpine'
  jenkins-weekly-digest:
    name: 'get latest image "jenkinsci/jenkins" digest'
    kind: 'dockerdigest'
    spec:
      image: 'jenkinsci/jenkins'
      tag: '{{ source "jenkins-weekly" }}'
    dependson:
      - 'jenkins-weekly'
targets:
  jenkins-weekly:
    name: 'deps: update Docker image "jenkinsci/jenkins" to "{{ source "jenkins-weekly" }}"'
    kind: 'yaml'
    spec:
      file: 'docker-compose.yaml'
      key: '$.services.jenkins-weekly.image'
    sourceid: 'jenkins-weekly-digest'
    transformers:
      - addprefix: 'jenkinsci/jenkins:'
`,
			},
		},
		{
			name:    "Scenario 2 - no digest",
			rootDir: "testdata",
			digest:  false,
			expectedPipelines: []string{`name: 'deps(dockercompose): bump "jenkinsci/jenkins" tag'
sources:
  jenkins-lts:
    name: 'get latest image tag for "jenkinsci/jenkins"'
    kind: 'dockerimage'
    spec:
      image: 'jenkinsci/jenkins'
      tagfilter: '^\d*(\.\d*){2}-alpine$'
      versionfilter:
        kind: 'semver'
        pattern: '>=2.150.1-alpine'
targets:
  jenkins-lts:
    name: 'deps: update Docker image "jenkinsci/jenkins" to "{{ source "jenkins-lts" }}"'
    kind: 'yaml'
    spec:
      file: 'docker-compose.yaml'
      key: '$.services.jenkins-lts.image'
    sourceid: 'jenkins-lts'
    transformers:
      - addprefix: 'jenkinsci/jenkins:'
`, `name: 'deps(dockercompose): bump "jenkinsci/jenkins" tag'
sources:
  jenkins-weekly:
    name: 'get latest image tag for "jenkinsci/jenkins"'
    kind: 'dockerimage'
    spec:
      architecture: 'amd64'
      image: 'jenkinsci/jenkins'
      tagfilter: '^\d*(\.\d*){1}-alpine$'
      versionfilter:
        kind: 'semver'
        pattern: '>=2.254-alpine'
targets:
  jenkins-weekly:
    name: 'deps: update Docker image "jenkinsci/jenkins" to "{{ source "jenkins-weekly" }}"'
    kind: 'yaml'
    spec:
      file: 'docker-compose.yaml'
      key: '$.services.jenkins-weekly.image'
    sourceid: 'jenkins-weekly'
    transformers:
      - addprefix: 'jenkinsci/jenkins:'
`,
			},
		},
	}

	for _, tt := range testdata {
		t.Run(tt.name, func(t *testing.T) {
			digest := tt.digest
			composefile, err := New(
				Spec{
					Digest: &digest,
				}, tt.rootDir, "", "")

			require.NoError(t, err)

			rawPipelines, err := composefile.DiscoverManifests()
			require.NoError(t, err)

			if len(rawPipelines) == 0 {
				t.Errorf("No pipelines found for %s", tt.name)
			}

			var pipelines []string
			assert.Equal(t, len(tt.expectedPipelines), len(rawPipelines))

			require.NoError(t, err)
			for i := range rawPipelines {
				// We expect manifest generated by the autodiscovery to use the yaml syntax
				pipelines = append(pipelines, string(rawPipelines[i]))
				assert.Equal(t, tt.expectedPipelines[i], pipelines[i])
			}

		})
	}

}
